package com.longrise.android.byjk.plugins.tabsecond;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.AllCourseChildBean;
import com.longrise.android.byjk.model.CourseDefaultItem;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.common.datasource.remote.LoadDataManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by callxkj on 2017/10/25.
 * 保险医学
 */

public class SecondDetailPresenter extends SecondDetailContract.Presenter{

    private List<CourseDefaultItem> mCourseDefaultItemList = new ArrayList<>();

    @Override
    protected void init() {

    }


    private void getDataFromNet(String traintype){
        String usersfzh = UserInfor.getInstance().getUsersfzh();
        EntityBean bean = new EntityBean();
        bean.set("assortmenttype", traintype);
        bean.set("areaid", "0");
        bean.set("cardno", usersfzh);
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService("", UrlConstants.BaseUrl,
                "bbt_course_sAllTrainPlanBeans", fbean, new LoadDataManager.OnRequestCallListener() {
                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        parseAllTrainData(result);
                    }

                    @Override
                    public void onError(String key, String service, Throwable ex, boolean isOnCallback) {

                    }

                    @Override
                    public void onFinished(String key, String service) {
                        mView.setRefreshing(false);
                    }
                });
    }


    private void parseAllTrainData(Object result) {
        try {
            if (result != null) {
                EntityBean bean = (EntityBean) result;
                int resultstate = bean.getInt("resultstate");
                if (resultstate == 1) {
                    mCourseDefaultItemList.clear();
                    EntityBean[] results = bean.getBeans("result");
                    for (EntityBean resultBean : results) {
                        AllCourseChildBean childBean = new AllCourseChildBean();
                        childBean.setRealpic(resultBean.getString("realpic"));
                        childBean.setSignupendtime(resultBean.getString("signupendtime"));
                        childBean.setGrouptype(resultBean.getString("grouptype"));
                        childBean.setGrouptypename(resultBean.getString("grouptypename"));
                        childBean.setCoursetype(resultBean.getString("coursetype"));
                        childBean.setCoursetypename(resultBean.getString("coursetypename"));
                        childBean.setCourseid(resultBean.getString("courseid"));
                        childBean.setName(resultBean.getString("name"));
                        childBean.setOriginprice(resultBean.getString("originprice"));
                        childBean.setRealprice(resultBean.getString("realprice"));
                        childBean.setGrouptime(resultBean.getString("grouptime"));
                        childBean.setCwtype(resultBean.getString("cwtype",""));

                        CourseDefaultItem item = new CourseDefaultItem(CourseDefaultItem.TYPE2);
                        item.setCourseChildBean(childBean);

                        mCourseDefaultItemList.add(item);
                    }
                    mView.refreshData(mCourseDefaultItemList);
                }else{
                    mView.showToast(bean.getString("resultdesc"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




    @Override
    void getData(String traintype) {
        mView.setRefreshing(true);
        getDataFromNet(traintype);
    }
}
